package com.nononsenseapps.feeder.ui.feed

import android.text.Spannable
import android.text.SpannableString
import android.text.style.TextAppearanceSpan
import android.view.View
import android.widget.ImageView
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.db.view.PreviewItem
import com.nononsenseapps.feeder.ui.common.RoundedCornerImageView
import kotlinx.coroutines.CoroutineScope

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
open class FeedItemHolder( view: View, actionCallback: ActionCallback,viewModel:Boolean) :
        FeedItemHolder_Mini(view,actionCallback,viewModel){

    init {
        imageView.radius= RoundedCornerImageView.Radius(imageView.dipsToPix(5F))
    }

     override fun fillTitle(forceRead: Boolean) {
        titleTextView.visibility = View.VISIBLE
        rssItem?.let { rssItem ->
            // \u2014 is a EM-dash, basically a long version of '-'
            val temps = if (rssItem.plainSnippet.isEmpty())
                rssItem.plainTitle
            else
                rssItem.plainTitle + " \n.\t\t" + rssItem.plainSnippet + "\u2026"
            val textSpan = SpannableString(temps)

            textSpan.setSpan(TextAppearanceSpan(view.context, R.style.TextAppearance_ListItem_Body),
                    rssItem.plainTitle.length, temps.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            if (rssItem.unread && !forceRead) {
                textSpan.setSpan(TextAppearanceSpan(view.context, R.style.TextAppearance_ListItem_Title),
                        0, rssItem.plainTitle.length,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            } else {
                textSpan.setSpan(TextAppearanceSpan(view.context, R.style.TextAppearance_ListItem_Title_Read),
                        0, rssItem.plainTitle.length,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            titleTextView.text = textSpan
        }
    }
}

interface ActionCallback {
    fun onDismiss(item: PreviewItem?)
    fun onSwipeStarted()
    fun onSwipeCancelled()
    fun coroutineScope(): CoroutineScope
    fun onItemClick(item: PreviewItem)
    fun markItemFeature(item: PreviewItem)
    fun markItemAttentionLevel(item: PreviewItem, attentionLevel:Int)
    fun onAuthorItemClick(item: PreviewItem)
    fun onEnclosureLinkClick(item: PreviewItem)
    fun onItemDelete(item: PreviewItem)
    fun showImagePreview(currentPosition:Int, urls: List<String>,srcView: ImageView?=null)
    fun onItemTagsEdit(item: PreviewItem)
    fun markAboveAllRead(item: PreviewItem)
    fun moveItemToFeed(item: PreviewItem, feedId: Long)
    fun markAsReadHistory(item: PreviewItem)
    fun showCurrentFeature(item: PreviewItem)

    fun navToReaderWebViewFragment(link:String?,enclosureLink:String?)
    fun navToReaderFragment()

    fun authorVisible():Boolean

    fun makeAllRead()
    fun generateQRCode(item: PreviewItem)
    fun generateEnclosureQRCode(item: PreviewItem)
    fun markAddFeedRule(item: PreviewItem, adBlock: Boolean=false, adImage: Boolean=false, custom: Boolean=false)

    fun speakFromCurrent(item: PreviewItem)
    fun markAsFavorite(it: PreviewItem)


    fun downLoadEnclosure(item: PreviewItem)
    fun pauseDownLoadEnclosure(item: PreviewItem)
    fun resumeDownLoadEnclosure(item: PreviewItem)
    fun delDownLoadEnclosure(item: PreviewItem)
    fun cancelDownLoadEnclosure(item: PreviewItem)

    fun preLoadState(item: PreviewItem): Int
    fun removeItemFeature(item: PreviewItem)
}

open class ActionCallBackAdapter(val scope: CoroutineScope):ActionCallback{
    override fun onDismiss(item: PreviewItem?) {}

    override fun onSwipeStarted() {}

    override fun onSwipeCancelled() {}

    override fun coroutineScope(): CoroutineScope {
        return scope
    }

    override fun onItemClick(item: PreviewItem) {}

    override fun markItemFeature(item: PreviewItem) { }

    override fun markItemAttentionLevel(item: PreviewItem, attentionLevel: Int) {}

    override fun onAuthorItemClick(item: PreviewItem) {}

    override fun onEnclosureLinkClick(item: PreviewItem) {}

    override fun onItemDelete(item: PreviewItem) {}

    override fun showImagePreview(currentPosition: Int, urls: List<String>, srcView: ImageView?) { }

    override fun onItemTagsEdit(item: PreviewItem) {}

    override fun markAboveAllRead(item: PreviewItem) {}

    override fun moveItemToFeed(item: PreviewItem, feedId: Long) {}

    override fun markAsReadHistory(item: PreviewItem) { }

    override fun showCurrentFeature(item: PreviewItem) {}

    override fun navToReaderWebViewFragment(link: String?, enclosureLink: String?) {}

    override fun navToReaderFragment() {}

    override fun authorVisible(): Boolean {
        return true
    }

    override fun makeAllRead() {}

    override fun generateQRCode(item: PreviewItem) {}

    override fun generateEnclosureQRCode(item: PreviewItem) {}

    override fun markAddFeedRule(
        item: PreviewItem,
        adBlock: Boolean,
        adImage: Boolean,
        custom: Boolean
    ) {}

    override fun speakFromCurrent(item: PreviewItem) {}
    override fun markAsFavorite(it: PreviewItem) {
    }

    override fun downLoadEnclosure(item: PreviewItem) {
    }

    override fun pauseDownLoadEnclosure(item: PreviewItem) {
    }

    override fun resumeDownLoadEnclosure(item: PreviewItem) {
    }

    override fun delDownLoadEnclosure(item: PreviewItem) {}
    override fun cancelDownLoadEnclosure(item: PreviewItem) {}
    override fun preLoadState(item: PreviewItem): Int = -1
    override fun removeItemFeature(item: PreviewItem) {
    }

}
